package com.huang.viewpage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.lang.ref.WeakReference;

public class DotIndicator extends View {
    private int dotColor = Color.GRAY;
    private int selectedColor = Color.BLUE;
    private float radius;// 圆角半径
    private boolean isSelected;
    private Paint paint;
//    private float six_times_rectF = 4f;
//    private float half_times_dots = six_times_rectF / 2f;
    private RectF rect;
    private WeakReference<Context> ctx;

    private void init(Context context) {
        paint = new Paint();
        paint.setAntiAlias(true);
        radius = 10f;// 圆角半径
        isSelected = false;
        selectedColor = ContextCompat.getColor(context, R.color.PaleTurquoise3);


        this.ctx = new WeakReference<Context>(context);

    }

    public DotIndicator(Context context) {
        super(context);
        init(context);
    }

    public DotIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void setSelected(boolean selected) {
        this.isSelected = selected;
        invalidate();  // 触发重绘
    }

    public DotIndicator setSelectedColor(int dotColor, int selectedColor) {
        this.dotColor = dotColor;
        this.selectedColor = selectedColor;
        invalidate();
        return this;
    }

//    public DotIndicator setTimesFactor(float timesFactor) {
//        this.six_times_rectF *= timesFactor;
//        this.half_times_dots = six_times_rectF / 2f;
//        return this;
//    }

    public DotIndicator setIndicatorHeight(float radius) {
        this.radius = dp2px(ctx.get(), radius);
        return this;
    }

    public DotIndicator setIndicatorWidth(float indicatorWidth) {
        this.right = dp2px(this.ctx.get(),indicatorWidth);
        return this;
    }
    private  float left = 0;
    private float top = 0;
    private float right = 60f;

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        float bottom = radius * 2;
        if (isSelected) {
            // 绘制矩形
            paint.setColor(selectedColor);
            if (rect == null) {
                rect = new RectF(left, top, right, bottom);
            }
            canvas.drawRoundRect(rect, radius, radius, paint);
        } else {
            // 绘制圆形
            paint.setColor(dotColor);
            canvas.drawCircle(right/2, radius, radius, paint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = (int) (right);
        int height = (int) (radius * 2);
        setMeasuredDimension(width, height);
    }

    protected int dp2px(Context mContext, float dp) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    protected int sp2px(Context mContext, float sp) {
        final float scale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * scale + 0.5f);
    }
}